package views;

import bean.Cls;
import bean.Student;
import service.ClassService;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RandomStudent extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_1;
    private boolean isRunning;
    private Thread taskThread;
    Cls cls2=new Cls();
    JButton btnNewButton = new JButton("随机");
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    RandomStudent frame = new RandomStudent();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public  void setCls(Cls cls) {
        cls2=cls;
    }
    private class RunnableTask implements Runnable {
        @Override
        public void run() {
            while (isRunning) {
                System.out.println("Task is running...");
                Student student=new Student();
                student.setC_id(cls2.getC_id());
                ClassService classService=new ClassService();
                Student student2=classService.selectStudent2(student);
                textField.setText(student2.getS_name());
                // Add a delay to avoid flooding the console (or any other resource)
                try {
                    Thread.sleep(200); // 1 second delay
                } catch (InterruptedException e) {
                    // Handle thread interruption (for stopping the task)
                    isRunning = false;
                }
            }
            System.out.println("Task stopped.");
        }
    }
    /**
     * Create the frame.
     */
    public RandomStudent() {
        setTitle("随机学生");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 549, 441);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblNewLabel = new JLabel("随机学生");
        lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 30));
        lblNewLabel.setBounds(187, 20, 153, 35);
        contentPane.add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("姓名：");
        lblNewLabel_1.setBounds(56, 77, 54, 15);
        contentPane.add(lblNewLabel_1);

        textField = new JTextField();
        textField.setBounds(120, 74, 306, 21);
        contentPane.add(textField);
        textField.setColumns(10);

        JLabel lblNewLabel_1_1 = new JLabel("头像 ：");
        lblNewLabel_1_1.setBounds(56, 178, 54, 15);
        contentPane.add(lblNewLabel_1_1);

        JLabel lblNewLabel_1_2 = new JLabel("分数：");
        lblNewLabel_1_2.setBounds(56, 332, 54, 15);
        contentPane.add(lblNewLabel_1_2);

        textField_1 = new JTextField();
        textField_1.setColumns(10);
        textField_1.setBounds(120, 329, 306, 21);
        contentPane.add(textField_1);

        JLabel lblNewLabel_2 = new JLabel();
        lblNewLabel_2.setBounds(120, 178, 153, 62);
        contentPane.add(lblNewLabel_2);

        ImageIcon image=new ImageIcon("..\\roll-call\\780.jpg");
        Image img = image.getImage();
        img = img.getScaledInstance(69, 72, Image.SCALE_DEFAULT);
        image.setImage(img);
        lblNewLabel_2.setIcon(image);
        getContentPane().add(lblNewLabel_2);


        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                if (isRunning) {
                    // Stop the task
                    isRunning = false;
                    if (taskThread != null) {
                        taskThread.interrupt();
                    }
                    btnNewButton.setText("随机");
                } else {
                    // Start the task
                    isRunning = true;
                    taskThread = new Thread(new RunnableTask());
                    taskThread.start();
                    btnNewButton.setText("停");
                }
            }

        });
        btnNewButton.setBounds(27, 371, 93, 23);
        contentPane.add(btnNewButton);

        JButton btnNewButton_1 = new JButton("评分");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null,"评分成功！！","提示",JOptionPane.PLAIN_MESSAGE);
            }
        });
        btnNewButton_1.setBounds(130, 371, 93, 23);
        contentPane.add(btnNewButton_1);

        JButton btnNewButton_1_1 = new JButton("请假");
        btnNewButton_1_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null,"评分成功！！","提示",JOptionPane.PLAIN_MESSAGE);
            }
        });
        btnNewButton_1_1.setBounds(233, 371, 93, 23);
        contentPane.add(btnNewButton_1_1);

        JButton btnNewButton_1_2 = new JButton("缺勤");
        btnNewButton_1_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null,"评分成功！！","提示",JOptionPane.PLAIN_MESSAGE);
            }
        });
        btnNewButton_1_2.setBounds(336, 371, 93, 23);
        contentPane.add(btnNewButton_1_2);

        JButton btnNewButton_1_3 = new JButton("返回");
        btnNewButton_1_3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Main m=new Main();
                m.setCls(cls2);
                m.setVisible(true);
                RandomStudent.this.dispose();
            }
        });
        btnNewButton_1_3.setBounds(439, 371, 93, 23);
        contentPane.add(btnNewButton_1_3);
    }

}
